<?php
/**
 * Created by PhpStorm.
 * User: pf
 * Date: 2017/12/6
 * Time: 11:07
 */

namespace App\Service;


use EasyWeChat\Core\Exception;
use GuzzleHttp\Client;

class WechatDeviceService
{
    protected $app;
    protected $http;
    protected $url = 'https://api.weixin.qq.com/shakearound/device/';

    public function __construct()
    {
        $this->app = app('wechat');
        $this->http = new Client();
    }

    protected function getAccessToken()
    {
        //得到access_token实例
        $accessToken = $this->app->access_token;
        return $accessToken->getToken();
    }

    //注册设备
    /*
     * @quantity 设备数量
     * @apply_reason 申请理由
     * @comment 备注
     * @poi_id  否
     * return
     *{
"data": {
               "apply_id": 123,
		"audit_status": 1,
		"audit_comment": "审核中"
   },
   "errcode": 0,
   "errmsg": "success."
}

     */
    public function applyid($quantity, $apply_reason, $comment, $poi_id = '')
    {

        $url = $this->url . 'applyid?access_token=' . $this->getAccessToken();
        $data = [
            "quantity" => (int)$quantity,
            "apply_reason" => $apply_reason,
            "comment" => $comment,
        ];
        $response = $this->http->post(
            $url, ['body' => json_encode($data)]
        );

        return json_decode((string)$response->getBody(), true);
    }
    //查询设备申请状态
    /*
     * apply_id 设备id
     */
    public function applystatus($apply_id)
    {
        $url = $this->url . '/applystatus?access_token=' . $this->getAccessToken();
        $response = $this->http->post(
            $url, ['body' => json_encode([
                'apply_id' => $apply_id
            ])]
        );
        return json_decode((string)$response->getBody(), true);
    }

    /**
     *编辑备注信息
     */
    public function update($device_id, $uuid, $major, $minor, $comment)
    {
        $url = $this->url . '/update?access_token=' . $this->getAccessToken();
        $response = $this->http->post(
            $url, ['body' => json_encode([
                "device_identifier" => [
                    "device_id" => $device_id,
                    "uuid" => $uuid,
                    "major" => $major,
                    "minor" => $minor
                ],
                "comment" => $comment
            ])]
        );
        return json_decode((string)$response->getBody(), true);
    }

    /**
     * 查询设备
     */
    public function search(array $data)
    {
        $url = $this->url . '/search?access_token=' . $this->getAccessToken();
        $response = $this->http->post(
            $url, ['body' => json_encode($data)]
        );
        return json_decode((string)$response->getBody(), true);
    }

    /**
     * @param $device_id
     * @param $uuid
     * @param $major
     * @param $minor
     * @param array $page_ids
     * @return mixed
     */
    public function bindpage($device_id, $uuid, $major, $minor, array $page_ids)
    {
        try{
            $url = $this->url . '/bindpage?access_token=' . $this->getAccessToken();
            $data = [
                'device_identifier' => compact('device_id', 'uuid', 'major', 'minor'),
                'page_ids' => $page_ids
            ];
            $response = $this->http->post(
                $url, ['body' => json_encode($data)]
            );
            return json_decode((string)$response->getBody(), true);
        }catch (Exception $e){
            return ['errcode'=>-1,'errmsg'  =>  $e->getMessage()];
        }



    }
    //新增设备分组
    public function addGroup($group_name){
        $url = $this->url . '/group/add?access_token=' . $this->getAccessToken();

        $data = [
            'group_name'=>$group_name
        ];

        $response = $this->http->post(
            $url, ['body' => json_encode($data)]
        );

        return json_decode((string)$response->getBody(), true);
    }
    //
    public function updateGroup($group_id,$group_name){
        $url = $this->url . '/group/add?access_token=' . $this->getAccessToken();

        $data = [
            'group_id'=>$group_id,
            'group_name'=>$group_name
        ];

        $response = $this->http->post(
            $url, ['body' => json_encode($data)]
        );

        return json_decode((string)$response->getBody(), true);

    }
    public function deleteGroup($group_id){
        $url = $this->url . '/group/delete?access_token=' . $this->getAccessToken();

        $data = [
            'group_id'=>$group_id,

        ];

        $response = $this->http->post(
            $url, ['body' => json_encode($data)]
        );

        return json_decode((string)$response->getBody(), true);

    }
    public function addDeviceToGroup($group_id, $device){
        $url = $this->url . '/group/adddevice?access_token=' . $this->getAccessToken();
        $device_ids=[];

        foreach ($device as $K=>$v){
            if(is_array($v)){
                $device_ids[$K]['device_id'] = (int)$v['device_id'];
            }else {
                $device_ids[$K]['device_id'] = (int)$v->device_id;
            }
        }
        $data = [
            'group_id'=>(int)$group_id,
            'device_identifiers'=>$device_ids
        ];
        $response = $this->http->post(
            $url, ['body' => json_encode($data)]
        );

        return json_decode((string)$response->getBody(), true);
    }
   public function groupGetDetail(){
       $url = $this->url . '/group/getdetail?access_token=' . $this->getAccessToken();
       $data = [
           "group_id"=>3338151,
            "begin"=>0,
             "count"=>100

       ];
       $response = $this->http->post(
           $url, ['body' => json_encode($data)]
       );

       return json_decode((string)$response->getBody(), true);
   }
}